package zxs.sino.elite.hub.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import zxs.sino.elite.hub.enums.user.LoginType;
import zxs.sino.elite.hub.model.form.LoginForm;
import zxs.sino.elite.hub.model.form.WXLoginForm;
import zxs.sino.elite.hub.service.UserAuthService;
import zxs.sino.elite.hub.utils.R;

import java.io.IOException;
import java.net.URISyntaxException;


/**
 * 用户认证管理
 *
 * @author zhang
 * @date 2025-06-30 20:51:41
 */
@RestController
@RequestMapping("auth/user")
@RequiredArgsConstructor
public class UserAuthController {

    private final UserAuthService userAuthService;

    /**
     * 微信小程序登录
     */
    @PostMapping("/appletLogin")
    public R appletLogin(@RequestBody WXLoginForm wxLoginForm) throws IOException, URISyntaxException {
        String token = userAuthService.appletLogin(wxLoginForm);

        return R.ok().put("token", token);
    }

    /**
     * 发送短信登录验证码
     */
    @PostMapping("/sendPhoneCode/{phone}")
    public R sendPhoneCode(@PathVariable String phone) {
        userAuthService.sendPhoneCode(phone);
        return R.ok();
    }

    /**
     * 手机号登录
     */
    @PostMapping("/phoneLogin")
    public R phoneLogin(@RequestBody LoginForm loginForm){
        String token = userAuthService.login(LoginType.PHONE.getCode(), loginForm);

        return R.ok().put("token", token);
    }

    @PutMapping("/updatePassword")
    public R updatePassword(@RequestBody LoginForm loginForm){
        userAuthService.updatePassword(loginForm);
        return R.ok();
    }

    @PostMapping("/logout")
    public R logout(){
        userAuthService.logout();
        return R.ok();
    }
}
